package org.jiamandan.com.config;

import cn.hutool.extra.spring.SpringUtil;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.DatagramChannel;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;


public class UdpServerInitializer extends ChannelInitializer<DatagramChannel> {
  private static final NettyProperties nettyProperties;

    static {
        nettyProperties = SpringUtil.getBean(NettyProperties.class);
    }


    @Override
    protected void initChannel(DatagramChannel ch) {
        ch.pipeline().addLast("idleStateHandler", new IdleStateHandler(nettyProperties.getReaderIdleTime(),
    0,
    0,
    TimeUnit.SECONDS))
                .addLast(new DatagramPacketToHexDecoder()) // 自定义解码器
                // 异常和状态处理
                .addLast(new UdpIdleEventHandler())
                .addLast(new UdpServerHandler());                // 业务处理器
    }
}